package com.myspring.web.view.excel;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.util.Assert;
import org.springframework.web.servlet.view.document.AbstractXlsView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Field;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * 如果是2007以上的excel，继承AbstractXlsxView
 * @author Administrator
 *
 */

public class ExcelView extends AbstractXlsView {
	@Override
	protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		response.setHeader("Content-Disposition", "attachment;filename="+ new String((DateFormatUtils.format(new Date(), "yyyyMMddHHmmss") + ".xls").getBytes(), "iso-8859-1"));
		
//		buildWorkbook1(model, workbook);

		buildWorkbook3(model, workbook);
	}


	public void buildWorkbook1(Map<String, Object> model, Workbook workbook) {
		//分割list
		for (Entry<String, Object> e : model.entrySet()) {
			HSSFSheet sheet = (HSSFSheet) workbook.createSheet("测试");

			if (e.getValue() instanceof List) {
				List<?> dataList = (List<?>) e.getValue();
				HSSFRow rowHeader = sheet.createRow(0);
				//添加header
				rowHeader.createCell(0).setCellValue("id");
				rowHeader.createCell(1).setCellValue("名字");

				for (int start = 0; start < dataList.size(); start++) {
					HSSFRow row = sheet.createRow(start + 1);
					//通过toString方法分列
					String[] rowsText = dataList.get(start).toString().split(",");
					for (int col = 0; col < rowsText.length; col++) {
						HSSFCell cell = row.createCell(col);
						cell.setCellValue(rowsText[col]);
					}
				}
			}

		}
	}


	public void buildWorkbook2(Map<String, Object> model, Workbook workbook) {
		Object header = model.get(ExeclConstants.EXCEL_HEADER);
		Assert.notNull(header, "表头不能为空");
		HSSFSheet sheet = this.buildHeader(header, workbook);

		try {
			for (Entry<String, Object> e : model.entrySet()) {
				//遍历value
				if (ExeclConstants.EXCEL_HEADER.equals(e.getKey()) && e.getValue() instanceof List) {
					List<?> dataList = (List<?>) e.getValue();
					//一个分页列表
					for (int start = 0; start < dataList.size(); start++) {
						HSSFRow row = sheet.createRow(start + 1);
						Object entry = dataList.get(start);
						Class<?> clazz = entry.getClass();
						Field[] fields = clazz.getDeclaredFields();
						for (Field field : fields) {
							Object obj = field.get(clazz.getConstructor().newInstance());
							System.out.println("student属性值：" + obj);

						}
					}
				}

			}
		} catch (Exception e ) {
			e.printStackTrace();
		}
	}

	/**
	 * 模板，key/value数据集情况
	 * @param model
	 * @param workbook
	 */
	public void buildWorkbook3(Map<String, Object> model, Workbook workbook) {
		Object header = model.get(ExeclConstants.EXCEL_HEADER);
		Assert.notNull(header, "表头不能为空");
		HSSFSheet sheet = this.buildHeader(header, workbook);

		try {
			Map<String, Object> contentMap = (Map<String, Object>) model.get(ExeclConstants.EXCEL_CONTENT);
			//遍历内容列表
			for (Entry<String, Object> e : contentMap.entrySet()) {
				//遍历value
				List<?> dataList = (List<?>) e.getValue();
				//一个分页列表
				for (int start = 0; start < dataList.size(); start++) {
					HSSFRow row = sheet.createRow(start + 1);
					Object entry = dataList.get(start);
					Class<?> clazz = entry.getClass();
					Field[] fields = clazz.getDeclaredFields();
					for (int i = 0; i < fields.length; i++) {
						Field field = fields[i];
						field.setAccessible(true);
						Object obj = field.get(entry);
						System.out.println("student属性值：" + obj);
						//转换的reportEntry全部弄成String类型
						HSSFCell cell = row.createCell(i);
						cell.setCellValue(String.valueOf(obj));
					}
				}
			}
		} catch (Exception e ) {
			e.printStackTrace();
		}
	}

	public HSSFSheet buildHeader(Object header, Workbook workbook) {
		HSSFSheet sheet = (HSSFSheet) workbook.createSheet("测试");
		if (header instanceof List) {
			List<String> headers = (List<String>) header;
			HSSFRow rowHeader = sheet.createRow(0);
			for (int i = 0; i < headers.size(); i++) {
				rowHeader.createCell(i).setCellValue(headers.get(i));
			}

		}
		return sheet;
	}
}
